package com.hsq.androidproject.ui.fragment;


import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.adapter.MyFragmentPagerAdapter;
import com.hsq.androidproject.ui.Users.ShowUsers;
import com.hsq.androidproject.ui.watching_fragment.WCacheFragment;
import com.hsq.androidproject.ui.watching_fragment.WKeepWatchFragment;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class WatchingFragment extends Fragment {
    Resources resources;
    private ViewPager mPager;
    private ArrayList<Fragment> fragmentsList;
    private TextView tv_recommend, tv_classify;
    private ImageView img_login;
    private TextView tv_bianji;
    private int currIndex = 0;
    private int offset = 0;
    private int position_one;
    WKeepWatchFragment wKeepWatchFragment;
    WCacheFragment wCacheFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment


        View view = inflater.inflate(R.layout.fragment_watching, container, false);
        resources = getResources();
        InitTextView(view);
        InitViewPager(view);
        initListerens();
        TranslateAnimation animation = new TranslateAnimation(position_one, offset, 0, 0);
        animation.setFillAfter(true);
        return view;
    }

    private void initListerens() {
        img_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), ShowUsers.class));
            }
        });
        tv_bianji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    private void InitTextView(View parentView) {
        tv_recommend = (TextView) parentView.findViewById(R.id.tv_recommend);
        tv_classify = (TextView) parentView.findViewById(R.id.tv_classify);
        img_login= (ImageView) parentView.findViewById(R.id.img_login);
        tv_bianji= (TextView) parentView.findViewById(R.id.tv_bianji);
        tv_recommend.setOnClickListener(new MyOnClickListener(0));
        tv_classify.setOnClickListener(new MyOnClickListener(1));
    }

    private void InitViewPager(View parentView) {
        mPager = (ViewPager) parentView.findViewById(R.id.vPager);
        fragmentsList = new ArrayList<>();
        wKeepWatchFragment = new WKeepWatchFragment();
        wCacheFragment = new WCacheFragment();
        fragmentsList.add(wKeepWatchFragment);
        fragmentsList.add(wCacheFragment);
        mPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentsList));
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());
        mPager.setCurrentItem(1);
    }

    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mPager.setCurrentItem(index);
        }
    }

    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    tv_recommend.setTextColor(Color.parseColor("#ffffff"));
                    tv_recommend.setBackgroundResource(R.drawable.shape_eg1);
                    tv_classify.setTextColor(Color.parseColor("#0dbe08"));
                    tv_classify.setBackgroundResource(R.drawable.shape_gs);

                    break;
                case 1:
                    tv_recommend.setTextColor(Color.parseColor("#0dbe08"));
                    tv_recommend.setBackgroundResource(R.drawable.shape_eg);
                    tv_classify.setTextColor(Color.parseColor("#ffffff"));
                    tv_classify.setBackgroundResource(R.drawable.shape_gs1);
                    break;
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }
}